package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.server.ServerCloneException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dao.order.pets.PetsDaoImpl;
import com.entity.Admins;
import com.entity.Cart;
import com.entity.CartItem;
import com.entity.Pets;
import com.entity.User;
import com.service.PetsService;
import com.service.PetsServiceImpl;

@Controller
public class PetsController {
	
	@Autowired
	private PetsService petsServiceImpl;
	
	
	@RequestMapping("petlist")
	public String findAll(Model model){
		List<Pets> list=petsServiceImpl.findAll();
		model.addAttribute("list", list);
		return "shop.jsp";
//		return "forward:/success.jsp";
//		return "redirect:/success.jsp";
		
		
	}
	@RequestMapping("finAll")
	public ModelAndView findAll1(Model model){
		List<Pets> list=petsServiceImpl.findAll();
		model.addAttribute("list", list);
		ModelAndView mv=new ModelAndView("/petslist.jsp");
		mv.addObject("list", list);
		return mv;	
	}

	
	@RequestMapping("toupdate")
	public String findPetsBId(Model model,@PathVariable String id){
		int ids=Integer.parseInt(id);
		System.out.println(ids);
		Pets product=petsServiceImpl.findPetsById(ids);
		model.addAttribute("pets", petsServiceImpl);//把该对象存储到request作用域中
		return "/petsinfo.jsp";
	}
	@RequestMapping("doupdate")
	public String updatePets(Pets p){
		//@RequestParam注解的三个属性
		petsServiceImpl.updatePets(p);
		
		return "Pets";
	}
	
	
	/*@RequestMapping("petslist")
	public String findAllPets(Model model,HttpServletRequest response){

		List<Pets> list=petsServiceImpl.findAll();
		model.addAttribute("list", list);
		return  JSON.toJSONString(list);
	}
	
	@RequestMapping("login")
	public Pets login(@RequestBody Pets pets){
		System.out.println(pets);
		
		return pets;
		
	}*/
	
	/*
	 * 调用Service中的分页方法；
	 */
	@RequestMapping("petsLists")
	public String PetsList(HttpServletRequest request,HttpServletRequest response)throws ServerCloneException,IOException{
		int pagenum=1;
		try {
			String page=request.getParameter("pagenum");
			String nowpage=request.getParameter("nowpage");
			if (nowpage!=null) {
				pagenum=Integer.valueOf(nowpage);
			} else {
				pagenum=Integer.valueOf(page);
			}
		} catch (Exception e) {
			pagenum=1;
		}
		request.setAttribute("page", petsServiceImpl.PetsPage(pagenum));
		return "shop.jsp";
	}
	@RequestMapping("addCart")
	public String PetsAddCart(@RequestParam("id")String id,HttpServletRequest request,HttpServletRequest response)throws ServerCloneException,IOException, ServletException{
		PetsDaoImpl petsDao = new PetsDaoImpl();
		Cart cart = new Cart();
		HttpSession session = request.getSession();
		if (session.getAttribute("cart")==null) {
			cart = new Cart();
		}else{
			cart = (Cart)session.getAttribute("cart");		
		}
		Pets pets = petsServiceImpl.select(Integer.valueOf(id));
		cart.addItem(pets);
		session.setAttribute("cart", cart);
		
		//request.getRequestDispatcher("addCart").forward(request, response);
		return "petsLists";
		
	}
	@RequestMapping("ClearCart")
	public String PetsClearCart(HttpServletRequest request,HttpServletResponse response)throws ServerCloneException,IOException, ServletException{
		HttpSession session = request.getSession();
		session.removeAttribute("cart");
		request.getRequestDispatcher("showcart.jsp").forward(request, response);
		return null;
	}
	@RequestMapping("DeleteCart")
	public String PetsDeleteCart(HttpServletRequest request,HttpServletResponse response)throws ServerCloneException,IOException, ServletException{
		int id = Integer.valueOf(request.getParameter("id"));
		HttpSession session = request.getSession();
		Cart cart = (Cart)session.getAttribute("cart");
		Map<Integer, CartItem> map = cart.getMap();
		System.out.println(map);
		map.remove(id);
		cart.setMap(map);
		session.setAttribute("cart", cart);
		request.getRequestDispatcher("showcart.jsp").forward(request, response);
		return null;
	}
	
	@RequestMapping("showCart")
	public String demo(){
		return "showcart.jsp";
	}

	@RequestMapping("CountCart")
	public String PetsCountCart(HttpSession session,HttpServletRequest request,HttpServletResponse response,int price)throws ServerCloneException,IOException, ServletException{
		PrintWriter out =response.getWriter();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		System.out.println(price+"总价");
		Admins admin = petsServiceImpl.admin(1);
		User user=petsServiceImpl.user(1);
		System.out.println(user+"controller层");
		if(user.getAccount()>=price){
			petsServiceImpl.subPrice(user.getId(), user.getAccount()-price);//用户余额减少
			admin.setAcount(admin.getAcount()+price);
			petsServiceImpl.updateAdmin(admin);//商家余额增加
			session.removeAttribute("cart");//结算后清空购物车	  
			out.print("<script charset=\"UTF-8\" language=\"javascript\">alert('支付成功');window.location.href='index.jsp'</script>");
		}else{
	    	out.print("<script charset=\"UTF-8\" language=\"javascript\">alert('您的余额不足');window.location.href='showcart.jsp'</script>");
		} 
		return null;
		}

}
